<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class ModelImages extends JModel
{

    /*
    private $hostname = "localhost";
    private $username = "sp3ctrum";
    private $password = "";
    private $db_name = "my_sp3ctrum";
    */
    
    private $hostname = "localhost";
    private $username = "root";
    private $password = "root";
    private $db_name = "gnu";
    /*
    private $hostname = "62.149.150.87";
    private $username = "Sql322031";
    private $password = "0a68934c";
    private $db_name = "Sql322031_5";
    */


    private $active = false;
    private $link;

    public function connect() {
        if(!$this->active) {
            $this->link = mysql_connect($this->hostname,$this->username,$this->password);
        }else {
            return true;
        }
    }

    public function disconnect() {
        if($this->active) {
            mysql_close();
            $this->active = false;
        }
    }

    public function insertImage($lat,$lon,$name,$path,$icon) {
        if(!$this->active) {
            $this->connect();
        }
        $db_selected = mysql_select_db($this->db_name, $this->link);
        if (!$db_selected) {
            die ('Can\'t use foo : ' . mysql_error());
        }
        $query = "INSERT INTO images VALUES('','$lat','$lon','$name','$path','$icon')";
        //echo $query;
        $sql = mysql_query($query) or die (mysql_error());
        return $sql;
    }

    public function getImages(){

        if(!$this->active) {
            $this->connect();
        }
        $db_selected = mysql_select_db($this->db_name, $this->link);
        if (!$db_selected) {
            die ('Can\'t use foo : ' . mysql_error());
        }

        $query = "SELECT id, filepath, name FROM `images`";

        $result = mysql_query($query);

        $images;
        if(mysql_numrows($result) >0 ){
        	$i=0;
            while ($i < mysql_numrows($result)) {
                $img = array(
                    "id"=>mysql_result($result,$i,"id"),
                    "image"=>mysql_result($result,$i,"filepath"),
                    "name"=>mysql_result($result,$i,"name")
                );

                $images[$i] = $img;
                $i++;
            }
        }

        return $images;
    }

    public function getImageDetails($id){

        if(!$this->active) {
            $this->connect();
        }
        $db_selected = mysql_select_db($this->db_name, $this->link);
        if (!$db_selected) {
            die ('Can\'t use foo : ' . mysql_error());
        }

        $query = "SELECT id, filepath, name FROM `images` WHERE id = '$id'";

        $result = mysql_query($query);

        $images;
        if(mysql_numrows($result) >0 ){

            $images = array(
                    "id"=>mysql_result($result, 0, "id"),
                    "image"=>mysql_result($result, 0, "filepath"),
                    "name"=>mysql_result($result, 0, "name")
                );
                
        }

        return $images;
    }

    public function retrieveImagesCount() {
        if(!$this->active) {
            $this->connect();
        }
        $db_selected = mysql_select_db($this->db_name, $this->link);
        if (!$db_selected) {
            die ("Errore nella selezione del db: " . mysql_error());
        }
        $query = "SELECT * FROM images";
        $result = mysql_query($query);

        return mysql_numrows($result);
    }

    public function retrieveMarkers() {
        if(!$this->active) {
            $this->connect();
        }
        $db_selected = mysql_select_db($this->db_name, $this->link);
        if (!$db_selected) {
            die ("Errore nella selezione del db: " . mysql_error());
        }
        $query = "SELECT * FROM images";
        $result = mysql_query($query);
        //init array to store marker informations
        $markers;

        if(mysql_numrows($result) > 0) {
            $i=0;
            while ($i < mysql_numrows($result)) {
                $marker = array(
                    "latitude"=>mysql_result($result,$i,"latitude"),
                    "longitude"=>mysql_result($result,$i,"longitude"),
                    "name"=>mysql_result($result,$i,"name"),
                    "filepath"=>mysql_result($result,$i,"filepath"),
                    "icon"=>mysql_result($result,$i,"icon")
                );

                $markers[$i] = $marker;
                $i++;
            }
        }

        return $markers;
    }

    public function checkImagePath($filepath){
        
        if(file_exists($filepath)){ //This function returns FALSE for files inaccessible due to safe mode restrictions.

            $str_split = explode(".",$filepath);
            $extension = $str_split[count($str_split)-1];
            if($extension == "jpg" || $extension == "JPG" || $extension == "jpeg" || $extension == "JPEG"){
                echo "$extension";
                return true;
            }
        }
        return false;
    }

}

